package net.wantongtech.xa.uploadbasicinfo.service;

import com.google.common.collect.Lists;
import com.wtkj.vo.dzzf.base.xxjh.MessageSend;
import lombok.extern.slf4j.Slf4j;
import net.wantongtech.xa.uploadbasicinfo.dao.ExportCCBBatchDeductionDao;
import org.apache.commons.io.FileUtils;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * 导出银行批量扣款数据服务
 */
@Slf4j
@Service
public class ExportCCBBatchDeductionService {


    private static final int PER_NUM = 100;
    private static final String DIR_PATH = "D:\\tools\\CCB";

    @Resource
    private ThreadPoolTaskExecutor taskExecutor;

    public void init() {
        File dir = new File(DIR_PATH);
        if (!dir.exists() && !dir.mkdirs()) {
            throw new IllegalStateException(String.format("文件夹路径%s创建失败", DIR_PATH));
        }
    }

    @Resource
    private ExportCCBBatchDeductionDao exportCCBBatchDeductionDao;

    public void export() {
        Integer total = exportCCBBatchDeductionDao.queryExportDataNum();
        log.info("共查询到{}条可导出数据", total);
        int num = ((int) Math.ceil((double) total / PER_NUM));
        log.info("每次查询{}条数据，共需要查询{}次", PER_NUM, num);
        List<Future<Void>> futureList = Lists.newArrayList();
        for (int i = 1; i <= num; i++) {
            int start = PER_NUM * (i - 1) + 1;
            int end = PER_NUM * i;
            if (end > total) {
                end = total;
            }
            Future<Void> future = taskExecutor.submit(new ExportDeductionThread(start, end));
            futureList.add(future);
        }
        for (Future<Void> future : futureList) {
            try {
                future.get();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                log.error(e.getMessage(), e);
            } catch (ExecutionException e) {
                log.error(e.getMessage());
            }
        }
        log.info("数据导出完成");
    }

    class ExportDeductionThread implements Callable<Void> {

        private final int start;

        private final int end;

        public ExportDeductionThread(int start, int end) {
            this.start = start;
            this.end = end;
        }

        @Override
        public Void call() {
            log.info("开始查询{}-{}的数据", start, end);
            List<MessageSend> sendList = exportCCBBatchDeductionDao.queryExportMsgSendByRowNum(start, end);
            log.info("共查询出{}条数据，开始处理...", sendList.size());
            for (MessageSend messageSend : sendList) {
                String msgBody = messageSend.getMsgBody();
                long msgId = messageSend.getMsgId();
                String fileName = String.format("%d.json", msgId);
                try {
                    FileUtils.writeStringToFile(new File(DIR_PATH, fileName), msgBody, "UTF-8");
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
            return null;
        }
    }


}
